/**********************************************************
* Description:                                            *
*         This demo show you how to implement accept      *
*         a command y/n without enter key.                *
*                                                         *
**********************************************************/
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char ch;
    struct termios old_term;
    struct termios cur_term;
    int ret;
    tcgetattr(STDIN_FILENO, &old_term);
    memcpy(&cur_term, &old_term, sizeof(cur_term));
    cur_term.c_lflag &= ~(ICANON);
    cur_term.c_cc[VMIN] = 1;
    cur_term.c_cc[VTIME] = 0;
    ret = tcsetattr(STDIN_FILENO, TCSANOW, &cur_term);
    if(ret < 0)
    {
        printf("Can't set\n");
        return 1;
    }
    printf("Are you OK?[y/n]");
    fflush(stdout);

    ret = read(STDIN_FILENO, &ch, 1);
    if(ret <= 0)
    {
        printf("Error\n");
    }
    else
    {
        if(ch == 'y' || ch == 'Y')
        {
            printf("\nYour choice is YES\n");
        }
        else if(ch == 'n' || ch == 'N')
        {
            printf("\nYour choice is NO\n");
        }
        else if(ch == '\n')
        {
            printf("\nYou make no choice, default to YES\n");
        }
        else
        {
            printf("\nInvalid choice\n");
        }
    }
    tcsetattr(STDIN_FILENO, TCSANOW, &old_term);

    return 0;
}
